package com.quzi.kettle.activitise.added;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quzi.kettle.GlobalConfig;
import com.quzi.kettle.R;
import com.quzi.kettle.activitise.BaseActivity;
import com.quzi.kettle.activitise.main.MainActivityTest;
import com.quzi.kettle.bean.ModeItem;
import com.quzi.kettle.datapiece.status.DeviceStatus;
import com.quzi.kettle.utils.MSharePerference;
import com.quzi.kettle.view.TitleBarHandler;
import com.quzi.kettle.view.TitleBarView;

import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

public class Modes2Activity extends BaseActivity {
	private TitleBarView titleView;
	private ModesEpAdapter adapter;
	private List<ModeItem> custom_modeLists;
	private ExpandableListView lv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_modes2);
		initTitleBar();
		initEplv();

	}

	@Override
	public void onClick(View v) {

	}

	private void initEplv() {

		/**
		 * 自定义模式
		 */
		custom_modeLists = getModeList();
		// String str =
		// MSharePerference.getInstance(getApplicationContext()).get(
		// MODES);
		// custom_modeLists = new Gson().fromJson(str,
		// new TypeToken<List<ModeItem>>() {
		// }.getType());
		// if (custom_modeLists == null) {
		// custom_modeLists = new ArrayList<ModeItem>();
		// }

		String[] modes = getResources().getStringArray(R.array.modes_type);

		List<TypeSpeMode> groundlists = new ArrayList<TypeSpeMode>();
		groundlists.add(new TypeSpeMode(R.drawable.custom, "自定义",
				new ArrayList<SpeMode>(), custom_modeLists));
		for (int i = 0; i < modes.length; i++) {
			switch (i) {
			case 0:

				getgroundlist_i(getResources()
						.getStringArray(R.array.milk_type), getResources()
						.getIntArray(R.array.milk_time), getResources()
						.getIntArray(R.array.milk_temp), groundlists,
						R.drawable.milk, modes[0]);

				break;
			case 1:

				getgroundlist_i(
						getResources().getStringArray(R.array.redtea_type),
						getResources().getIntArray(R.array.redtea_time),
						getResources().getIntArray(R.array.redtea_temp),
						groundlists, R.drawable.redtea, modes[1]);

				break;
			case 2:

				getgroundlist_i(
						getResources().getStringArray(R.array.whitetea_type),
						getResources().getIntArray(R.array.whitetea_time),
						getResources().getIntArray(R.array.whitetea_temp),
						groundlists, R.drawable.whitetea, modes[2]);

				break;
			case 3:

				getgroundlist_i(
						getResources().getStringArray(R.array.yellowtea_type),
						getResources().getIntArray(R.array.yellowtea_time),
						getResources().getIntArray(R.array.yellowtea_temp),
						groundlists, R.drawable.yellowtea, modes[3]);

				break;
			case 4:

				getgroundlist_i(
						getResources().getStringArray(R.array.blacktea_type),
						getResources().getIntArray(R.array.blacktea_time),
						getResources().getIntArray(R.array.blacktea_temp),
						groundlists, R.drawable.blacktea, modes[4]);
				break;

			case 5:

				getgroundlist_i(getResources()
						.getStringArray(R.array.puer_type), getResources()
						.getIntArray(R.array.puer_time), getResources()
						.getIntArray(R.array.puer_temp), groundlists,
						R.drawable.puertea, modes[5]);

				break;
			case 6:

				getgroundlist_i(
						getResources().getStringArray(R.array.coffee_type),
						getResources().getIntArray(R.array.coffee_time),
						getResources().getIntArray(R.array.coffee_temp),
						groundlists, R.drawable.coffeetea, modes[6]);

				break;
			case 7:

				getgroundlist_i(
						getResources().getStringArray(R.array.greentea_type),
						getResources().getIntArray(R.array.greentea_time),
						getResources().getIntArray(R.array.greentea_temp),
						groundlists, R.drawable.greentea, modes[7]);

				break;
			case 8:

				getgroundlist_i(
						getResources().getStringArray(R.array.wulongtea_type),
						getResources().getIntArray(R.array.wulongtea_time),
						getResources().getIntArray(R.array.wulongtea_temp),
						groundlists, R.drawable.wulongtea, modes[8]);

				break;
			case 9:

				getgroundlist_i(
						getResources().getStringArray(R.array.flowertea_type),
						getResources().getIntArray(R.array.flowertea_time),
						getResources().getIntArray(R.array.flowertea_temp),
						groundlists, R.drawable.flowertea, modes[9]);

				break;

			case 10:

				getgroundlist_i(
						getResources().getStringArray(
								R.array.flowerglasstea_type),
						getResources().getIntArray(R.array.flowerglasstea_time),
						getResources().getIntArray(R.array.flowerglasstea_temp),
						groundlists, R.drawable.flowerglasstea, modes[10]);
				break;
			case 11:

				getgroundlist_i(getResources()
						.getStringArray(R.array.soap_type), getResources()
						.getIntArray(R.array.soap_time), getResources()
						.getIntArray(R.array.soap_temp), groundlists,
						R.drawable.soap, modes[11]);

				break;
			default:
				break;
			}
		}
		// Arrays.asList(modes);
		adapter = new ModesEpAdapter(groundlists, getApplicationContext());
		adapter.setIeplistener(new IEpListener() {

			@Override
			public void modeclick(DeviceStatus status, String name, int temp) {
				updateMode(status, name, temp);
				MainActivityTest.needSaveMode = false;
				MainActivityTest.isStart = false;
				finish();
				// nextActivity(MainActivityTest.class);

			}
		});
		lv = (ExpandableListView) findViewById(R.id.eplv_modes2_expandlv);
		lv.setGroupIndicator(null);
		lv.setAdapter(adapter);

	}

	protected void updateMode(DeviceStatus deviceStatus, String string,
			int targetTemperature) {
		modeStatus = deviceStatus;

		modeStatus.setTemperature = (byte) targetTemperature;
		MSharePerference.getInstance(getApplicationContext()).update(
				GlobalConfig.MODE, string);
		MSharePerference.getInstance(getApplicationContext()).update(
				GlobalConfig.TargetTemp, "" + targetTemperature);
	}

	private void getgroundlist_i(String[] type, int[] time, int[] temp,
			List<TypeSpeMode> groundlists, int draw_resid, String mode) {
		if (!(type.length == time.length) && !(time.length == temp.length)) {
			return;
		}
		List<SpeMode> list_soap = new ArrayList<SpeMode>();
		for (int j = 0; j < type.length; j++) {
			list_soap.add(new SpeMode(type[j], time[j], temp[j]));
		}
		groundlists.add(new TypeSpeMode(draw_resid, mode, list_soap, null));
	}

	@Override
	protected void initTitleBar() {
		super.initTitleBar();
		titleView = (TitleBarView) findViewById(R.id.titleview);
		titleView.setTitle(getResources().getString(R.string.mode));
		titleView.setTitleBarClickListener(new TitleBarHandler() {

			@Override
			public void ClickRight() {

			}

			@Override
			public void ClickLeft() {
				finish();
			}
		});
	}
}
